package com.action.log;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.log.LogInfo;
import com.business.log.LogInfoBus;
import com.config.SystemGlobals;
import com.util.HtmlEncode;
import com.util.TextTools;

public class ManageLog extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);

		if (request.getParameter("deleted") != null) {
			String[] logNos = request.getParameterValues("deleted");
			LogInfoBus lib = new LogInfoBus();
			lib.del(logNos);
			out
					.println("<script>alert(\"删除日志成功\");window.location=\"admin/blog/loglist.jsp\";</script>");
			out.close();
		}
		if (request.getParameter("edit") != null) {
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			HtmlEncode he = new HtmlEncode();
			content = he.SimpleEncodeme(content);
			String isQuote = request.getParameter("isquote");
			String isShow = request.getParameter("isshow");
			String logAuthor = request.getParameter("logauthor");
			String logCategory = request.getParameter("logcategory");
			String logIntro = request.getParameter("intro");
			String logTag = request.getParameter("logtag");
			logIntro = TextTools.txt2html(logIntro);
			LogInfo loginfo = new LogInfo();
			loginfo.setLogIntro(logIntro);
			loginfo.setLogCategory(logCategory);
			if (isShow == null || isShow.equals("")) {
				isShow = "0";
			}
			loginfo.setIsShow(isShow);
			loginfo.setLogAuthor(logAuthor);
			loginfo.setLogContent(content);

			Date date = new Date();
			Timestamp nowsTime = new Timestamp(date.getTime());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			if (isQuote == null || isQuote.equals("")) {
				isQuote = "0";
			}
			loginfo.setIsQuote(isQuote);
			loginfo.setLogPostTime(sdf.format(nowsTime));
			loginfo.setLogTitle(title);
			String logtypeId = request.getParameter("logtypeid");
			loginfo.setLogtypeId(logtypeId);
			loginfo.setReplynum(request.getParameter("replynum"));
			loginfo.setLogQuotenums(request.getParameter("log_quotenums"));
			loginfo.setViewnums(request.getParameter("viewnums"));
			loginfo.setLogQuoteId(request.getParameter("log_quote_id"));
			loginfo.setLogTag(logTag);
			LogInfoBus lis = new LogInfoBus();
			String logNo = request.getParameter("logno");
			lis.updateLog(loginfo, logNo);
			out
					.println("<script>alert(\"编辑日志内容成功\");window.location=\"admin/blog/loglist.jsp\";</script>");
			out.close();
		}
	}

}
